﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain.Departments;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyWrite.API.Appicant.Command.Departs;
using MediatR;

namespace B.S.DataBase.MyWrite.API.Appicant.CommandHandler.Departs
{
    /// <summary>
    /// 部门添加处理方法
    /// </summary>
    public class DepartAddCommandHandler : IRequestHandler<DepartAddCommand, APIResult<object>>
    {
        /// <summary>
        /// 部门仓储接口
        /// </summary>
        private readonly IBaseRepository<DepartModel> dRepo;
        /// <summary>
        /// 映射
        /// </summary>
        private readonly IMapper mapper;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="dRepo"></param>
        /// <param name="mapper"></param>
        public DepartAddCommandHandler(IBaseRepository<DepartModel>dRepo,IMapper mapper)
        {
            this.dRepo = dRepo;
            this.mapper = mapper;
        }


        /// <summary>
        /// 处理方法
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<object>> Handle(DepartAddCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            result.Code = APIEnum.添加成功;
            result.Msg = APIEnum.添加成功.ToString();
            var model = mapper.Map<DepartModel>(request);
            await dRepo.Add(model);
            result.data = model;    
            return await Task.FromResult(result);


        }



    }



}
